﻿CREATE PROCEDURE [dbo].[CashRegisterShiftsUpdate]
(
	@Note varchar(250),
	@MD datetime,
	@MW varchar(50),
	@Original_Id int,
	@Original_MD datetime
)
--Закрытие кассовой смены через процедуру CloseCashRegisterShift
AS
DECLARE @strMsg nvarchar(500), @intRet int, @intErrCode int
SET NOCOUNT ON

SELECT @intRet = Id
FROM [CD].[CashRegisterShifts] WHERE Id = @Original_Id AND MD = @Original_MD
IF (@@rowcount <> 1)
BEGIN
	SELECT @intErrCode = 1, 
	@strMsg = 'Данные изменены после последнего запроса!'
	RAISERROR (@strMsg, 11, 1)
	RETURN @intErrCode
END

EXEC [dbo].[ProcessStatistic] @Note OUTPUT, @MD OUTPUT, @MW OUTPUT
	SET NOCOUNT OFF;
UPDATE [CD].[CashRegisterShifts] SET [Note] = @Note, [MD] = @MD, [MW] = @MW 
WHERE Id = @Original_Id AND MD = @Original_MD;
	
SELECT Id, DepartmentId, CashRegisterId, OpenUserId, OpenDT, CloseUserId, CloseDT, Note, MD, MW 
FROM CD.CashRegisterShifts WHERE (Id = @Original_Id)

RETURN 0